package com.zmc.dynamic.shard.rule;

import com.google.common.collect.Range;

import com.zmc.dynamic.shard.rule.service.AbstractShardingRuleService;
import com.zmc.dynamic.shard.rule.service.Impl.ShardingRuleFactory;
import org.springframework.util.CollectionUtils;

import java.util.*;


public class RouteRule {

    /**
     * 精确查询
     * @param collection : 分表集合
     * @param valuesMap : 分片健集合 (key:分片健，value:值)
     *
     * */
    public static Set<String> getTablesByShardingValues(Collection<String> collection, Map<String, Collection<String>> valuesMap) {
        Set<String> tables = new HashSet<>();
        valuesMap.forEach((key, value) -> {
            Collection<String> values = valuesMap.get(key);
            if (values != null && !CollectionUtils.isEmpty(values)) {
                //简单工厂+策略模式
                Set<String> result = new ShardingRuleFactory().getRule(collection).doShard(collection, key, values);
                tables.addAll(result);
            }
        });
        return tables;
    }

    /**
     * 范围查询
     * @param collection : 分表集合
     * @param rangeValuesMap : 分片范围集合
     *
     * */
    public static Collection<String> getTablesByRangeValues(Collection<String> collection, Map<String, Range<String>> rangeValuesMap) {
        Set<String> tables = new HashSet<>();
        rangeValuesMap.forEach((key, value) -> {
            Range<String> values = rangeValuesMap.get(key);
            if (values != null) {

            }
        });
        return tables;
    }
}
